package com.ruoyi.huanjing.controller;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import ucar.ma2.Array;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class Read1D {

    @Anonymous
    @GetMapping("/read1D")
    public Map<String, Object> read1D(String filePath, String variableName) throws IOException {
        filePath = "E:\\localfile\\MYD021KM.A2017314.0345.061.2017314164621.hdf";
        variableName = "Band_250M";
        try (NetcdfFile ncFile = NetcdfFile.open(filePath)) {
            // 获取变量
            Variable thermalDetectorVar = ncFile.findVariable(variableName);

            if (thermalDetectorVar == null) {
                System.err.println("变量" + variableName + "未找到");
                return null;
            }

            // 读取数据
            Array thermalData = thermalDetectorVar.read();
            // 将 Array 转换为 short 数组
            short[] result = new short[(int) thermalData.getSize()];
            for (int i = 0; i < thermalData.getSize(); i++) {
                result[i] = thermalData.getShort(i);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("height", result);
            map.put("values", result);
            return map;
        } catch (IOException e) {
            System.err.println("处理文件时出错: " + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
}
